package com.video4s.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.video4s.utils.PropetiesUtils;

public abstract class BaseDAO {

	protected Connection conn;
	protected Statement stmt;
	protected PreparedStatement pStmt;
	protected ResultSet rs;
	protected CallableStatement cStmt;
	
	public synchronized Connection getConnection() throws Exception {
		try {
			String classJDBC = PropetiesUtils.loadValue("classJDBC");
			String driverJDBC = PropetiesUtils.loadValue("driverJDBC");
			String usernameJDBC = PropetiesUtils.loadValue("usernameJDBC");
			String passwordJDBC = PropetiesUtils.loadValue("passwordJDBC");
			Class.forName(classJDBC);
			conn = DriverManager.getConnection(driverJDBC, usernameJDBC,passwordJDBC);
			return conn;
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
	}

	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (pStmt != null) {
				pStmt.close();
			}
			if (cStmt != null) {
				cStmt.close();
			}
			if (conn != null) {
				conn.close();
			}

		} catch (Exception e) {
			rs = null;
			stmt = null;
			pStmt = null;
			cStmt = null;
			conn = null;
		}
	}
}
